package com.superbug.moi.stupiccqupt.ui.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.gc.materialdesign.views.ButtonRectangle;
import com.superbug.moi.stupiccqupt.R;

public class SearchPicFragment extends Fragment implements View.OnClickListener {

    private EditText mEditText;
    private ButtonRectangle searchPic;
    private ImageView mImageView;
    private ImageLoader.ImageListener listener;
    private ImageLoader imageLoader;
    private InputMethodManager inputMethodManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_pic, container, false);

        RequestQueue mQueue = Volley.newRequestQueue(view.getContext());

        imageLoader = new ImageLoader(mQueue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return null;
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {

            }
        });

        init(view);
        return view;
    }

    private void init(View view) {
        mEditText = (EditText) view.findViewById(R.id.ed_student_id);
        mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
        searchPic = (ButtonRectangle) view.findViewById(R.id.btn_search_pic);
        searchPic.setBackgroundColor(getResources().getColor(R.color.primary_color));
        searchPic.setOnClickListener(this);
        mImageView = (ImageView) view.findViewById(R.id.iv_stuPic);
        listener = ImageLoader.getImageListener(mImageView, R.drawable.loading, R.drawable.error);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_search_pic:
                String stuId;
                stuId = mEditText.getText().toString();
                if (stuId.equals("")) {
                    mEditText.setError("你至少给我输入点数字呗");
                } else if (stuId.length() != 10) {
                    mEditText.setError("我们家学号都是10位的好不好");
                } else {
                    Log.d("---->","http://jwzx.cqupt.edu.cn/showstuPic.php?xh=" + stuId);
                    imageLoader.get("http://jwzx.cqupt.edu.cn/showstuPic.php?xh=" + stuId,
                            listener);
                    inputMethodManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
                }
                break;
        }
    }
}
